import config from "./config";

let ajaxTimes = 0;
export default function (params) {

  //判断url中是否带有/me/ 请求的是私有路径，带上header
  let header = {
    ...params
  };
  // if (params.url.includes('/me/')) {
  //   //拼接一下header 带上token
  //   header["token"] = wx.getStorageSync('token')
  // }
  if (!params.method) {
    params.method = 'GET'
  }


  //显示加载中
  ajaxTimes++;
  wx.showLoading({
    title: 'loading',
    mask: true
  });

  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      header: header,
      url: config.host + ':' + config.port + params.url,
      method: params.method,
      data: params.data,

      success: (result) => {
        resolve(result.data)
      },

      fail: (err) => {
        reject(err);
      },

      complete: () => {
        ajaxTimes--;
        if (ajaxTimes === 0)
          wx.hideLoading()
      }
    })
  })
};
